package org.geometerplus.fbreader.fbreader;


import com.chineseall.reader.ui.util.GlobalApp;
import com.chineseall.singlebook.R;

import org.geometerplus.zlibrary.core.fonts.FontEntry;
import org.geometerplus.zlibrary.ui.android.view.ZLAndroidPaintContext;

import java.util.HashMap;
import java.util.List;

import static org.geometerplus.fbreader.fbreader.DrawBuy.NO_NET_WORK_REFRESH;

/**
 * Created by Tony on 2018/12/4.
 */

public class DrawBuyView {

    private static volatile DrawBuyView mDrawBuyView;

    private FBView mFBView;

    private int topY;

    private List<FontEntry> myFontEntry;

    private ZLAndroidPaintContext contexts;

    private boolean mNeedPay;

    private HashMap<DrawBuy, TapZoneMap.RectangleZone> drawBuyRectangleZoneHashMap;

    private DrawBuyView() {

    }


    public static DrawBuyView with() {
        if (mDrawBuyView == null) {
            synchronized (DrawBuyView.class) {
                if (mDrawBuyView == null) {
                    mDrawBuyView = new DrawBuyView();
                }
            }
        }
        return mDrawBuyView;
    }

    public DrawBuyView setZLAndroidPaintContext(ZLAndroidPaintContext contexts) {
        this.contexts = contexts;
        return mDrawBuyView;
    }

    public DrawBuyView setFBView(FBView fbView) {
        this.mFBView = fbView;
        return mDrawBuyView;
    }

    public DrawBuyView setNeedPay(boolean needPay) {
        this.mNeedPay = needPay;
        return mDrawBuyView;
    }

    public DrawBuyView setTopY(int topY) {
        this.topY = topY;
        return mDrawBuyView;
    }

    public DrawBuyView setFontEntrys(List<FontEntry> myFontEntry) {
        this.myFontEntry = myFontEntry;
        return mDrawBuyView;
    }

    public DrawBuyView setRectangleZone(HashMap<DrawBuy, TapZoneMap.RectangleZone> drawBuyRectangleZoneHashMap) {
        this.drawBuyRectangleZoneHashMap = drawBuyRectangleZoneHashMap;
        return mDrawBuyView;
    }


    public DrawBuyChapterTips builder() {
        return new DrawBuyChapterTips();
    }


    class DrawBuyChapterTips {

        private String mTitle;
        private String currTopName;
        private String mPrice;
        private String mByBook;
        private String mPre;//优惠推荐
        private String openVip;
        private int payType;
        private byte[] desc;
        private String hint;

        private String noWorkHint;

        public DrawBuyChapterTips() {
            mPre = GlobalApp.getInstance().getString(R.string.txt_buy_pre);
            openVip = GlobalApp.getInstance().getString(R.string.txt_buy_open_vip);
            mByBook = GlobalApp.getInstance().getString(R.string.txt_buy_by_chapter);
            noWorkHint = GlobalApp.getInstance().getString(R.string.txt_buy_no_work_hint);
            this.mPrice = GlobalApp.getInstance().getString(R.string.txt_buy_price, "10", "100");
        }

        public String getmTitle() {
            return mTitle;
        }

        public DrawBuyChapterTips setmTitle(String mTitle) {
            this.mTitle = mTitle;
            return this;
        }

        public String getCurrTopName() {
            return currTopName;
        }

        public DrawBuyChapterTips setCurrTopName(String currTopName) {
            this.currTopName = currTopName;
            return this;
        }

        public String getmPrice() {
            return mPrice;
        }

        public DrawBuyChapterTips setmPrice(String chapterPrice, String balance) {
            this.mPrice = GlobalApp.getInstance().getString(R.string.txt_buy_price, chapterPrice, balance);
            return this;
        }

        public String getmByBook() {
            return mByBook;
        }

        public DrawBuyChapterTips setmByBookType(int byBookType) {
            switch (byBookType) {
                case 2:
                    mByBook = GlobalApp.getInstance().getString(R.string.txt_buy_by_books);
                    hint = GlobalApp.getInstance().getString(R.string.txt_buy_hint);
                    break;
                default:
                    mByBook = GlobalApp.getInstance().getString(R.string.txt_buy_by_chapter);
                    hint = "";
                    break;
            }

            return this;
        }

        public String getmPre() {
            return mPre;
        }

        public void setmPre(String mPre) {
            this.mPre = mPre;
        }

        public String getOpenVip() {
            return openVip;
        }

        public int getPayType() {
            return payType;
        }

        public DrawBuyChapterTips setPayType(int payType) {
            this.payType = payType;
            return this;
        }

        public void setOpenVip(String openVip) {
            this.openVip = openVip;
        }

        public byte[] getDesc() {
            return desc;
        }

        public void setDesc(byte[] desc) {
            this.desc = desc;
        }

        /**
         * 开始绘制
         */
        public void draw() {
            draw(mNeedPay);
        }

        /**
         * 开始绘制
         *
         * @param needPay
         */
        private void draw(boolean needPay) {
            if (needPay) {
                drawNetwork();
            } else {
                drawNoNetwork();
            }
        }

        /**
         * 绘制弱网或者网络异常的情况
         */
        private void drawNoNetwork() {
            //绘制标题
            DrawBuy.DRAW_TITLE.draw(mFBView, contexts, topY, currTopName, currTopName, myFontEntry, null);

            NO_NET_WORK_REFRESH.draw(mFBView, contexts, topY, currTopName, noWorkHint, myFontEntry, drawBuyRectangleZoneHashMap.get(NO_NET_WORK_REFRESH));
        }

        /**
         * 绘制有网络的情况或者网络良好的情况
         */
        private void drawNetwork() {
            //绘制标题
            DrawBuy.DRAW_TITLE.draw(mFBView, contexts, topY, currTopName, currTopName, myFontEntry, null);
            //绘制内容
            DrawBuy.DRAW_CONTENT.draw(mFBView, contexts, topY, currTopName, mTitle, myFontEntry, null);

            //绘制阴影
            DrawBuy.DRA_SHADOW.draw(mFBView, contexts, topY, currTopName, "", myFontEntry, null);
            //绘制价格
            DrawBuy.DRAW_PRICE.draw(mFBView, contexts, topY, hint, mPrice, myFontEntry, null);
            //绘制购买
            DrawBuy.DRAW_BUY.draw(mFBView, contexts, topY, currTopName, mByBook, myFontEntry, drawBuyRectangleZoneHashMap.get(DrawBuy.DRAW_BUY));
            //判断是否是整本购买，如果是整本购买则不显示整本购买
            if (payType != 2) {
                //绘制优惠
                DrawBuy.DRAW_BATCH_BUY.draw(mFBView, contexts, topY, currTopName, mPre, myFontEntry, drawBuyRectangleZoneHashMap.get(DrawBuy.DRAW_BATCH_BUY));
            }
            //绘制线条
            DrawBuy.DRAW_LINE.draw(mFBView, contexts, topY, currTopName, "", myFontEntry, null);
            //绘制开通VIP
            DrawBuy.DRAW_OPEN_VIP.draw(mFBView, contexts, topY, currTopName, openVip, myFontEntry, drawBuyRectangleZoneHashMap.get(DrawBuy.DRAW_OPEN_VIP));

        }

    }


}
